package com.neusoft.a312cxy.android18cxy.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import com.neusoft.a312cxy.android18cxy.R;

 /**
 * Created by Young on 2017/9/28.
 */

public class LoginActivity extends AppCompatActivity
{
    private Button btn_reg;
    private EditText et_user;
    private EditText et_pass;
    private Button btn_log;
    private CheckBox logincheckBox;
    private SharedPreferences sp;
    private SharedPreferences sid;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mainn);
        sp = getSharedPreferences("loginInfo", MODE_PRIVATE);
        sid = getSharedPreferences("userinfo", MODE_PRIVATE);
        initView();
        ReadSp();
        initEvents();
        initData ();


    }
    public void  initView(){
        et_pass=(EditText)findViewById(R.id.pass);
        btn_reg=(Button)findViewById(R.id.regbutton);
        et_user=(EditText)findViewById(R.id.user_comment_shopname);
        btn_log=(Button)findViewById(R.id.login);
        logincheckBox=(CheckBox)findViewById(R.id.checkBox);
    }
    private void readSP() {
        String userName = sp.getString("userName", null);
        String pwd = sp.getString("pwd", null);
        Boolean rm = sp.getBoolean("remember", false);
        if (rm) {
            et_user.setText(userName);
            et_pass.setText(pwd);
            logincheckBox.setChecked(true);
        } else {
            logincheckBox.setChecked(false);
        }
    }
    public void  initEvents(){
        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//

                Intent intent1=new Intent(LoginActivity.this,registerActivity.class);
                startActivity(intent1);


            }
        });


        btn_log.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                startActivity(intent);

//               Retrofit retrofit=new Retrofit.Builder().
//                       baseUrl("http://172.24.10.130:8080/foodService/")
//                      .addConverterFactory(GsonConverterFactory.create())
//                       .build();
//               UserService service=retrofit.create(UserService.class);
//                Call call =service.login(et_user.getText().toString(),et_pass.getText().toString());
               /* UserModel userModel=new UserModel();
                Call call=userModel.login(et_user.getText().toString(),et_pass.getText().toString());
                Callback<UserResult> callback=new Callback<UserResult>() {
                    @Override
                    public void onResponse(Call<UserResult> call, Response<UserResult> response) {
                        UserResult userResult=response.body();
                        String userid=userResult.getUserid();
                        if("0".equals(userid))
                        {
                            Toast.makeText(LoginActivity.this, "登陆失败", Toast.LENGTH_SHORT).show();

                        }
                        else{
                            SaveSp();

                            SharedPreferences.Editor editor = sid.edit();
                            editor.putString("user_id", userid);
                            editor.commit();
                            Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                            startActivity(intent);

                            Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();


                        }
                    }

                    @Override
                    public void onFailure(Call<UserResult> call, Throwable t) {
                        Toast.makeText(LoginActivity.this, "登陆失败2", Toast.LENGTH_SHORT).show();

                    }

                };
                call.enqueue(callback);
*/

            }
        });


    }


    public void initData (){

    }
    public  void SaveSp(){
        SharedPreferences.Editor editor = sp.edit();

        editor.putString("userName", et_user.getText().toString());
        editor.putString("pwd", et_pass.getText().toString());
        editor.putBoolean("remember", logincheckBox.isChecked());
        editor.commit();
    }
    public void ReadSp(){

        String userName = sp.getString("userName", null);
        String pwd = sp.getString("pwd", null);
        Boolean rm = sp.getBoolean("remember", false);
        if (rm) {
            et_user.setText(userName);
            et_pass.setText(pwd);
            logincheckBox.setChecked(true);
        } else {
            logincheckBox.setChecked(false);
        }

    }
    public void saveUser(String user_id)
    {
        SharedPreferences sharedPreferences9=getSharedPreferences("userinfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor =sharedPreferences9.edit();
        editor.putString("username",et_user.getText().toString());
        int userid=Integer.parseInt(user_id);
        editor.putInt("user_id",userid);
        editor.putString("userpass",et_pass.getText().toString());
        editor.commit();
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1&&resultCode==RESULT_OK) {
            String username = data.getStringExtra("username");
            et_user.setText(username);
        }
    }


}
